/**
* @desc 函数防抖
* @param func 目标函数
* @param wait 延迟执行毫秒数
*/
export function debounce(func, wait = 240) {
  let timeout = null
  return function() {
    const context = this
    const args = arguments
    if (timeout) clearTimeout(timeout)
    timeout = setTimeout(() => {
      func.apply(context, args)
    }, wait)
  }
}
// export function debounce(fn, wait = 260) {
//   let timeout = null
//   return function() {
//     if (timeout !== null) clearTimeout(timeout)
//     timeout = setTimeout(fn, wait)
//   }
// }

/**
* @desc 函数节流
* @param func 函数
* @param wait 延迟执行毫秒数
*/
export function throttle(func, wait) {
  let timeout = null
  return function() {
    const context = this
    const args = arguments
    if (!timeout) {
      timeout = setTimeout(() => {
        timeout = null
        func.apply(context, args)
      }, wait)
    }
  }
}

export function filterimg(value) {
  if (!value) return ''
  value = value.toString()
  if (value.slice(-4) === 'jpeg') {
    return (
      'https://cube.elemecdn.com/' +
      value.charAt(0) +
      '/' +
      value.slice(1, 3) +
      '/' +
      value.slice(3) +
      '.jpeg?x-oss-process=image/format,webp/resize,w_150'
    )
  } else if (value.slice(-3) === 'png') {
    return (
      'https://cube.elemecdn.com/' +
      value.charAt(0) +
      '/' +
      value.slice(1, 3) +
      '/' +
      value.slice(3) +
      '.png?x-oss-process=image/format,webp/resize,w_750'
    )
  } else if (value.slice(-3) === 'jpg') {
    return (
      'https://cube.elemecdn.com/' +
      value.charAt(0) +
      '/' +
      value.slice(1, 3) +
      '/' +
      value.slice(3) +
      '.jpg?x-oss-process=image/resize,m_lfit,w_120/watermark,g_se,x_4,y_4,image_YS8xYS82OGRlYzVjYTE0YjU1ZjJlZmFhYmIxMjM4Y2ZkZXBuZy5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8yOA%3D%3D/quality,q_90/format,webp'
    )
  } else {
    return value
  }
}
